package cn.iocoder.yudao.module.dbit.controller.admin.jobtitles;

import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;


import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.dbit.controller.admin.jobtitles.vo.*;
import cn.iocoder.yudao.module.dbit.dal.dataobject.jobtitles.JobTitlesDO;
import cn.iocoder.yudao.module.dbit.service.jobtitles.JobTitlesService;

@Tag(name = "管理后台 - 职位名称")
@RestController
@RequestMapping("/dbit/job-titles")
@Validated
public class JobTitlesController {

    @Resource
    private JobTitlesService jobTitlesService;

    @PostMapping("/create")
    @Operation(summary = "创建职位名称")
    @PreAuthorize("@ss.hasPermission('dbit:job-titles:create')")
    public CommonResult<Integer> createJobTitles(@Valid @RequestBody JobTitlesSaveReqVO createReqVO) {
        return success(jobTitlesService.createJobTitles(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新职位名称")
    @PreAuthorize("@ss.hasPermission('dbit:job-titles:update')")
    public CommonResult<Boolean> updateJobTitles(@Valid @RequestBody JobTitlesSaveReqVO updateReqVO) {
        jobTitlesService.updateJobTitles(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除职位名称")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('dbit:job-titles:delete')")
    public CommonResult<Boolean> deleteJobTitles(@RequestParam("id") Integer id) {
        jobTitlesService.deleteJobTitles(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得职位名称")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('dbit:job-titles:query')")
    public CommonResult<JobTitlesRespVO> getJobTitles(@RequestParam("id") Integer id) {
        JobTitlesDO jobTitles = jobTitlesService.getJobTitles(id);
        return success(BeanUtils.toBean(jobTitles, JobTitlesRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得职位名称分页")
    @PreAuthorize("@ss.hasPermission('dbit:job-titles:query')")
    public CommonResult<PageResult<JobTitlesRespVO>> getJobTitlesPage(@Valid JobTitlesPageReqVO pageReqVO) {
        PageResult<JobTitlesDO> pageResult = jobTitlesService.getJobTitlesPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, JobTitlesRespVO.class));
    }

    @GetMapping("/list")
    @Operation(summary = "获得职位列表")
    public CommonResult<List<JobTitlesSelectVO>> getJobTitlesList() {
        List<JobTitlesDO> resutl = jobTitlesService.getJobTitlesList();
        return success(BeanUtils.toBean(resutl, JobTitlesSelectVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出职位名称 Excel")
    @PreAuthorize("@ss.hasPermission('dbit:job-titles:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportJobTitlesExcel(@Valid JobTitlesPageReqVO pageReqVO,
                                     HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<JobTitlesDO> list = jobTitlesService.getJobTitlesPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "职位名称.xls", "数据", JobTitlesRespVO.class,
                BeanUtils.toBean(list, JobTitlesRespVO.class));
    }

}